<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Cookie;
use think\Db;

class Index extends Base
{
    public function index()
    {
        $admin = Cookie::get('userName');
        $id = Cookie::get('id');
        $img = Db::name('user_info')->field('img')->where('user_id', $id)->find();
        $data = [
            'user' => $admin,
            'img' => $img['img'],
        ];
        $this->assign('user', $data);
        return $this->fetch();
    }

    public function show(){
        $count = Db::name('admin')->count();
        $admin = Cookie::get('userName');
        $last_login_time = Db::name('admin')->field('last_login_time')->where('user', $admin)->find();
        $data = [
            'count' => $count,
            'time' => $last_login_time['last_login_time'],
        ];
        $this->assign('user', $data);
        return $this->fetch('show');
    }

    public function hotNews(){
        $hot_news = Db::name('news')->field('mid, author, title')->limit(5)->order('mid DESC')->select();
        // dump($hot_news);
        $data = [
            "data" => $hot_news,
        ];

        return json($data);
    }

    public function hotCases(){
        $hot_cases = Db::name('case')->field('cid, content, title')->limit(5)->order('cid DESC')->select();
        $data = [
            "data" => $hot_cases,
        ];

        return json($data);
    }

    // 获取基本信息
    public function baseData(){
        $version = PHP_VERSION;
        $server = PHP_OS;
        $host = $_SERVER['HTTP_HOST'];
        $data = [
            'version' => $version,
            'homePage' => $host,
            'server' => $server,
        ];
        return json($data);
    }
}
